package com.gcsoftware.web.threads;

import com.gcsoftware.web.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by qs on 2018/4/9.
 */
@Component
public class FileUploadThread extends Thread {

    @Autowired
    private static FileUploadService service ;
    private static volatile Queue<String> roleQue= initRoleQueue();

    public static void setRoleQue(final String roleId) {
        FileUploadThread.roleQue.offer(roleId);
    }

    public static void setService(FileUploadService service) {
        if (null == FileUploadThread.service) {
            synchronized (FileUploadThread.class) {
                if (null == FileUploadThread.service) {
                    FileUploadThread.service = service;
                }
            }
        }
    }




    private static Queue<String> initRoleQueue () {
        if (null == roleQue) {
            synchronized (FileUploadThread.class) {
                if (null == roleQue) {
                    roleQue = new LinkedList<String>();
                }
            }
        }
        return roleQue;
    }

    @Override
    public void run() {
        System.out.println("Start Thread...");
        while (true) {
            //System.out.println(roleQue.size());
            if (null != this.roleQue && this.roleQue.size() > 0) {
                String roleId = this.roleQue.poll();
                this.service.fileUpload(roleId);
            }
        }
    }
}
